package com.heroCW.bodhiAttic.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heroCW.bodhiAttic.dto.OrdersDto;
import com.heroCW.bodhiAttic.entity.Orders;
import com.heroCW.bodhiAttic.resultUtil.Result;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.OrderDetailService;
import com.heroCW.bodhiAttic.service.OrderService;
import com.heroCW.bodhiAttic.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     *
     * @return
     */
    @PostMapping("/submit")
    public Result<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        Boolean flag = orderService.submit(orders);
        if (flag) {
            return Result.success(ResultCode.SAVE_OK, "下单成功");
        } else {
            return Result.error(ResultCode.SAVE_ERR, "下单失败");
        }
    }

    /**
     * 后端订单明细
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<OrdersDto>> page(int page, int pageSize, String number, String beginTime, String endTime) {
        log.info("page:{}, pageSize:{}, number: {}, startTime: {}, endTime: {}", page, pageSize, number, beginTime, endTime);

        IPage<OrdersDto> ordersDtoPage = orderService.getPage(page, pageSize, number, beginTime, endTime);
        if (ordersDtoPage != null) {
            return Result.success(ResultCode.GET_OK, ordersDtoPage);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到订单数据");
        }
    }

    /**
     * 订单状态修改
     *
     * @param map
     * @return
     */
    @PutMapping
    public Result<String> send(@RequestBody Map<String, String> map) {
        log.info("map: {}", map.toString());
        Integer status = Integer.parseInt(map.get("status"));
        Long orderId = Long.valueOf(map.get("id"));

        Boolean flag = orderService.updateOrder(orderId, status);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "订单状态修改成功");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "订单状态修改失败");
        }
    }

    /**
     * 查询用户界面的历史订单
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public Result<IPage<OrdersDto>> userPage(Integer page, Integer pageSize) {
        log.info("page:{}, pageSize:{}", page, pageSize);

        IPage<OrdersDto> ordersDtoPage = orderService.getOrderPage(page, pageSize);
        if (ordersDtoPage != null) {
            return Result.success(ResultCode.GET_OK, ordersDtoPage);
        } else {
            return Result.error(ResultCode.GET_ERR, "没有找到订单数据");
        }
    }

    /**
     * 再来一单功能
     *
     * @param map
     * @return
     */
    @PostMapping("/again")
    public Result<String> oneMoreOrder(@RequestBody Map<String, String> map) {
        Long orderId = Long.valueOf(map.get("id"));
        log.info("orderId:{}", orderId);

        Boolean flag = orderService.oneMoreOrder(orderId);
        if (flag) {
            return Result.success(ResultCode.SAVE_OK, "感谢再次光临");
        } else {
            return Result.error(ResultCode.SAVE_ERR, "再来一单失败");
        }
    }

    /**
     * 取消订单功能
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/cancel")
    public Result<String> cancelOrder(Long id, Integer status) {
        log.info("id: {}, status: {}", id, status);

        Boolean flag = orderService.updateOrder(id, status);
        if (flag) {
            return Result.success(ResultCode.UPDATE_OK, "订单取消完成");
        } else {
            return Result.error(ResultCode.UPDATE_ERR, "订单取消失败");
        }
    }
}
